﻿using System;
using System.Collections.Generic;
using System.IO;
using IQIGame.Onigao.Framework.Serialization;
using IQIGame.Onigao.GamePlay.Editors;
using IQIGame.Onigao.Logic;

namespace IQIGame.Onigao.GamePlay
{
    public static class LevelServiceEditorConfigFactory
    {
        public static LevelServiceCustomConfig CreateTriggerConfig(ServiceTriggerType rType, ByteBuf _buf)
        {
            var rTypeStr = "IQIGame.Onigao.GamePlay.LevelServiceTriggerCustom_" + rType.ToString();
            var rConfigType = Type.GetType(rTypeStr);
            LevelServiceCustomConfig rConfig = null;
            if (rConfigType != null)
            {
                rConfig = Activator.CreateInstance(rConfigType, true) as LevelServiceCustomConfig;
            }
            SerializeByteBufUtil.DeSerializeObject(_buf, ref rConfig);
            return rConfig;
        }

        public static LevelServiceCustomConfig CreateExecutionConfig(ServiceExecutionType rType, ByteBuf _buf)
        {
            var rTypeStr = "IQIGame.Onigao.GamePlay.LevelServiceExecutionCustom_" + rType.ToString();
            var rConfigType = Type.GetType(rTypeStr);
            LevelServiceCustomConfig rConfig = null;
            if (rConfigType != null)
            {
                rConfig = Activator.CreateInstance(rConfigType, true) as LevelServiceCustomConfig;
            }
            SerializeByteBufUtil.DeSerializeObject(_buf, ref rConfig);
            return rConfig;
        }

        private static Dictionary<string, EditorLevelServicePrefabSource> servicePrefabCache = new Dictionary<string, EditorLevelServicePrefabSource>();

        public static EditorLevelServicePrefabSource GetServiceUnitPrefab(string key)
        {
            if (servicePrefabCache.Count <= 0)
            {
                var rPath = LevelServiceBlackboard.ServicePrefabPath;
                if (File.Exists(rPath))
                {
                    var rBuf = new ByteBuf(File.ReadAllBytes(rPath));
                    var rList = rBuf.DeSerializeList<EditorLevelServicePrefabSource>();
                    foreach (var rConfig in rList)
                    {
                        servicePrefabCache.Add(rConfig.key, rConfig);
                    }
                }
            }
            if (servicePrefabCache.TryGetValue(key, out var rSource))
            {
                return rSource;
            }
            return null;
        }

        private static Dictionary<string, EditorLevelServiceExecPrefabSource> execPrefabCache = new Dictionary<string, EditorLevelServiceExecPrefabSource>();

        public static EditorLevelServiceExecPrefabSource GetExecutionPrefab(string key)
        {
            if (execPrefabCache.Count <= 0)
            {
                var rPath = LevelServiceBlackboard.ExecutionPrefabPath;
                if (File.Exists(rPath))
                {
                    var rBuf = new ByteBuf(File.ReadAllBytes(rPath));
                    var rList = rBuf.DeSerializeList<EditorLevelServiceExecPrefabSource>();
                    foreach (var rConfig in rList)
                    {
                        execPrefabCache.Add(rConfig.key, rConfig);
                    }
                }
            }
            if (execPrefabCache.TryGetValue(key, out var rSource))
            {
                return rSource;
            }
            return null;
        }

        public static void ClearPrefabCache()
        {
            servicePrefabCache.Clear();
            execPrefabCache.Clear();
        }
    }
}